package com.spgf.controller.fragments;

import java.text.NumberFormat;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.archtecture.model.exceptions.NegocioException;
import com.archtecture.model.patterns.PadraoFragmentMnt;
import com.spgf.controller.CategoriaCadActivity;
import com.spgf.controller.R;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.enums.Mes;
import com.spgf.model.rn.CategoriaRN;
import com.spgf.model.utils.ConstantesSPGF;
import com.spgf.view.utils.AdapterListViewCategorias;

public class RelatorioCategoriaFragment extends PadraoFragmentMnt<CategoriaModel> implements OnItemLongClickListener {

	private TextView	txtTotal;

	@SuppressLint("SimpleDateFormat")
	public void atualizarLista() throws NegocioException {

		Double valorTotal = 0.0;
		for (final CategoriaModel lModelTemp : getListaModel()) {
			valorTotal += lModelTemp.getValorTotal();
		}

		txtTotal.setText(NumberFormat.getCurrencyInstance().format(valorTotal));
	}

	@Override
	public void configurarTela(LayoutInflater inflater, ViewGroup container) throws NegocioException {

		rootView = inflater.inflate(R.layout.activity_relatorio_categoria, container, false);

		listview = (ListView) rootView.findViewById(R.id.lst_relatorio_categorias);
		listview.setScrollbarFadingEnabled(true);
		listview.setOnItemLongClickListener(this);

		txtTotal = (TextView) rootView.findViewById(R.id.lbl_relatorio_categoria_valor_total);
		txtTotal.setText(NumberFormat.getCurrencyInstance().format(0D));

		atualizarLista();

		listview.setAdapter(new AdapterListViewCategorias(rootView.getContext(), getListaModel()));
	}

	@Override
	protected void executarExcluir(Integer pCodigo) throws NegocioException {}

	@Override
	@SuppressWarnings("rawtypes")
	protected boolean executarLongClick(AdapterView pParent, View pView, int pPosition, long pID) throws NegocioException {
		return false;
	}

	@Override
	public Class<?> getControleCadastro() {
		return CategoriaCadActivity.class;
	}

	@Override
	protected List<CategoriaModel> getListaModel() throws NegocioException {

		Bundle args = getArguments();

		Mes lMes = Mes.getEnum(args.getInt(ConstantesSPGF.ARG_OBJECT));
		String lAno = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));

		return new CategoriaRN(rootView.getContext()).obterResumoMensalCategorias(lMes, lAno);
	}

}